#include "gamewindow.h"

//gameWindow::gameWindow(QWidget *parent) : QWidget(parent)
//{

//}


gameWindow::gameWindow(int levelNum)
{
    this->m_leveNum=levelNum;
    this->is_win=true;
    resize(320,600);
    this->setWindowIcon(QIcon(":/coin/res/Coin0001.png"));
    this->setWindowTitle("翻金币");

    QLabel *winLabel = new QLabel(this);
    QPixmap pix;
    pix.load(":/background/res/LevelCompletedDialogBg.png");
    winLabel->setFixedSize(pix.width(),pix.height());
    winLabel->setPixmap(pix);
    winLabel->move((this->width()-winLabel->width())*0.5,-winLabel->height());


    myPushButton* backBtn = new myPushButton(":/button/res/BackButton.png",":/button/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&myPushButton::clicked,[=](){
        emit this->backChoseWindow();
    });

    QLabel* levelLabel = new QLabel(this);
    levelLabel->setGeometry(0,this->height()-80,150,80);
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(20);
    levelLabel->setFont(font);
    QString leveInfo = QString("Level:%1").arg(this->m_leveNum);
    levelLabel->setText(leveInfo);

    dataConfig data;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->coinData[i][j]=data.mData[this->m_leveNum][i][j];
        }
    }

    for(int i=0;i<16;i++)
    {
        QLabel* coinLabel=new QLabel;
        QPixmap pix;
        pix.load(":/coin/res/BoardNode.png");
        coinLabel->setParent(this);
        coinLabel->setFixedSize(pix.width(),pix.height());
        coinLabel->setPixmap(pix);
        coinLabel->move(57+pix.width()*(i%4),200+pix.height()*(i/4));
    }
    for(int i=0;i<16;i++)
    {
        Coin* coin=NULL;
        if(coinData[i%4][i/4]==1)
        {
             coin = new Coin(":/coin/res/Coin0001.png");
             coin->m_flag=true;
        }
        else
        {
             coin = new Coin(":/coin/res/Coin0008.png");
             coin->m_flag=false;
        }
        coinPix[i%4][i/4]=coin;
        coin->m_x=i%4;
        coin->m_y=i/4;
        connect(coin,&QPushButton::clicked,[=](){
            coin->changeCoin();
            this->coinData[coin->m_x][coin->m_y]=!this->coinData[coin->m_x][coin->m_y];
            if(coin->m_x+1<4)
            {
                coinPix[coin->m_x+1][coin->m_y]->changeCoin();
                this->coinData[coin->m_x+1][coin->m_y]=!this->coinData[coin->m_x+1][coin->m_y];
            }
            if(coin->m_x-1>-1)
            {
                coinPix[coin->m_x-1][coin->m_y]->changeCoin();
                this->coinData[coin->m_x-1][coin->m_y]=!this->coinData[coin->m_x-1][coin->m_y];
            }
            if(coin->m_y+1<4)
            {
                coinPix[coin->m_x][coin->m_y+1]->changeCoin();
                this->coinData[coin->m_x][coin->m_y+1]=!this->coinData[coin->m_x][coin->m_y+1];
            }
            if(coin->m_y-1<4)
            {
                coinPix[coin->m_x][coin->m_y-1]->changeCoin();
                this->coinData[coin->m_x][coin->m_y-1]=!this->coinData[coin->m_x][coin->m_y-1];
            }

            QTimer::singleShot(50,[=](){
                this->is_win=true;
                for(int a=0;a<4;a++)
                {
                    for(int b=0;b<4;b++)
                    {
                        if(coinData[a][b]==0)
                        {
                            this->is_win=false;
                        }
                    }
                }
                if(this->is_win==true)
                {
                    for(int j=0;j<4;j++)
                    {
                        for(int k=0;k<4;k++)
                        {
                            coinPix[j][k]->is_win=true;
                        }
                    }
                    QPropertyAnimation* animation = new QPropertyAnimation(winLabel,"geometry");
                    animation->setDuration(1000);
                    animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                    animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                    animation->setEasingCurve(QEasingCurve::OutBounce);
                    animation->start();
                }
            });

        });

        coin->setParent(this);
        coin->move(60+50*(i%4),205+50*(i/4));
    }
}

void gameWindow::paintEvent(QPaintEvent *)
{
    QPixmap pix;
    QPainter painter(this);

    pix.load(":/background/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
